﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeaBattle.Domain.Entities
{
    public class Fleet
    {
        private readonly byte MaxShipCount;
        private readonly byte MaxPartCount;

        private Dictionary<Ship,ShipOrientation> ships = new Dictionary<Ship,ShipOrientation>();

        /// <summary>
        /// Initializes a new instance of the <see cref="Fleet"/> class.
        /// </summary>
        /// <param name="maxShipCount">The max ship count.</param>
        /// <param name="maxPartCount">The max part count.</param>
        /// <remarks></remarks>
        public Fleet(byte maxShipCount, byte maxPartCount)
        {
            this.MaxShipCount = maxShipCount;
            this.MaxPartCount = maxPartCount;
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        /// <remarks></remarks>
        public int Id { get; set; }

        /// <summary>
        /// Determines whether this instance [can add ship] the specified ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        /// <returns><c>true</c> if this instance [can add ship] the specified ship; otherwise, <c>false</c>.</returns>
        /// <remarks></remarks>
        private bool CanAddShip(Ship ship)
        {
            if (ships.Count + 1 > MaxShipCount)
                return false;
            if (ships.Sum(x=>x.Key.PartsCount) + ship.PartsCount > MaxPartCount)
             return false;
            return true;
        }

        /// <summary>
        /// Adds the ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        /// <param name="orientation">The orientation.</param>
        /// <remarks></remarks>
        public void AddShip(Ship ship,ShipOrientation orientation)
        {
            if (CanAddShip(ship))
            {
                ships.Add(ship,orientation);
            }
        }

        /// <summary>
        /// Removes the ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        /// <remarks></remarks>
        public void RemoveShip(Ship ship)
        {
            ships.Remove(ship);
        }

        /// <summary>
        /// Resets the fleet.
        /// </summary>
        /// <remarks></remarks>
        public void ResetFleet()
        {
            ships.Clear();
        }
    }

    public enum ShipOrientation
    {
        ORIENTATION_HORIZONTAL = 0,
        ORIENTATION_VERTICAL
    }
}
